from django.contrib.admin.templatetags.admin_list import pagination, paginator_number
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.pagination import PageNumberPagination
from rest_framework import status
from .models import User,UserDetail,Consumption
from .serializer import UserSerializer,UserDetailSerializer,ConsumptionSerializer

# 封装添加人员详情的接口
class UserDetailAdd(APIView):
    def post(self,request):
        user=request.data.get('user')
        hobby=request.data.get('hobby')
        address=request.data.get('address')

        if not all([hobby,address]):
            return Response({'error':'爱好和地址不能输入空'},status=status.HTTP_400_BAD_REQUEST)

        if UserDetail.objects.filter(user=user).exists():
            return Response({'error':'用户不能重复'},status=status.HTTP_400_BAD_REQUEST)

        serializer = UserDetailSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response({'succeed':'添加成功'})

# 封装添加消费表记录的接口
class ConsumptionAdd(APIView):
    def post(self,request):
        goods=request.data.get('goods')
        user=request.data.get('user')
        price=request.data.get('price')
        nums=request.data.get('nums')

        if not all([goods,price,nums]):
            return Response({'error':'不能输入空'},status=status.HTTP_400_BAD_REQUEST)

        serializer = ConsumptionSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response({'succeed':'添加成功'},status=status.HTTP_200_OK)

# 封装消费信息查询的接口
class ConsumptionList(APIView):
    def get(self, request):
        user=Consumption.objects.all()
        paginator=FenYe()
        result=paginator.paginate_queryset(user,request)
        serializer = ConsumptionSerializer(result,many=True)
        return Response(serializer.data)

class FenYe(PageNumberPagination):
    page_size = 3
    page_size_query_param = 'page_size'
    max_page_size = 3

    def get_page_number(self, request,paginator):
        page_number = request.query_params.get(self.page_size_query_param,1)
        try:
            page_number = int(page_number)
            if page_number < 1:
                page_number = 1
            elif page_number > paginator.num_pages:
                page_number = paginator.num_pages
        except ValueError:
            page_number = 1
        return page_number

# 封装人员信息查询的接口
class UserDetailData(APIView):
    def get(self, request,user):
        try:
            use=UserDetail.objects.get(user=user)
            serializer=UserDetailSerializer(use)
            return Response(serializer.data,status=status.HTTP_200_OK)
        except User.DoesNotExist:
            return Response({'error':'不存在'})

# 封装人员消费信息统计数据
class ConsumptionTJ(APIView):
    def get(self, request,user):
        try:
            use=Consumption.objects.get(user=user)
            serializer=ConsumptionSerializer(use)
            return Response(serializer.data,status=status.HTTP_200_OK)
        except User.DoesNotExist:
            return Response({'error':'不存在'})
